package tt.dz.biz.service.user;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import tt.dz.biz.dao.user.OperRoleDAO;
import tt.dz.common.BusinessException;
import tt.dz.common.DateUtil;
import tt.dz.common.DzErrorCode;
import tt.dz.common.DzErrorCode.CommonErrorCode;
import tt.dz.common.LogUtil;
import tt.dz.vo.OperListVO;
import tt.dz.vo.OperRoleVO;
import tt.dz.vo.RoleVO;

@Service("operRoleService")
public class OperRoleService
{
  @Resource(name="operRoleDAO")
  OperRoleDAO operRoleDAO;
  
  public List<OperRoleVO> listAllOperRole()
  {
    List<OperRoleVO> operRoleList = null;
    try
    {
      operRoleList = this.operRoleDAO.listAllOperRole();
    }
    catch (Exception e)
    {
      LogUtil.error("查询操作员角色关联列表异常", e);
      operRoleList = new ArrayList();
    }
    return operRoleList;
  }
  
  public List<OperRoleVO> queryOperRoleByRole(OperRoleVO operRoleVO)
  {
    List<OperRoleVO> operRoleList = null;
    try
    {
      operRoleList = this.operRoleDAO.queryOperRoleByRole(operRoleVO);
    }
    catch (Exception e)
    {
      LogUtil.error("查询操作员角色关联列表异常", e);
      operRoleList = new ArrayList();
    }
    return operRoleList;
  }
  
  public List<OperRoleVO> queryOperRoleByOper(OperListVO operListVO)
  {
    List<OperRoleVO> operRoleList = null;
    try
    {
      operRoleList = this.operRoleDAO.queryOperRoleByOper(operListVO);
    }
    catch (Exception e)
    {
      LogUtil.error("查询操作员角色关联列表异常", e);
      operRoleList = new ArrayList();
    }
    return operRoleList;
  }
  
  public List<RoleVO> queryUnAllotRole(OperListVO operListVO)
  {
    List<RoleVO> roleList = null;
    try
    {
      roleList = this.operRoleDAO.queryUnAllotRole(operListVO);
    }
    catch (Exception e)
    {
      LogUtil.error("查询操作员角色关联列表异常", e);
      roleList = new ArrayList();
    }
    return roleList;
  }
  
  public boolean modifyOperRole(List<OperRoleVO> operRoleList, String managerId)
  {
    boolean excuteFlag = true;
    try
    {
      this.operRoleDAO.deleteOperRole(managerId);
      int[] iArray = this.operRoleDAO.batchInsertOperRole(operRoleList);
      for (int i : iArray) {
        if (i <= 0)
        {
          excuteFlag = false;
          break;
        }
      }
    }
    catch (Exception e)
    {
      excuteFlag = false;
      LogUtil.error("批量插入操作员角色关联异常", e);
    }
    return excuteFlag;
  }
  
  public void modifyOperRoleForBizSystem(String managerId, String sysCode, List<OperRoleVO> operRoleList)
    throws Exception
  {
    try
    {
      this.operRoleDAO.deleteOperRole(managerId, sysCode);
      OperRoleVO operRoleVO = null;
      OperRoleVO tmp = null;
      int n = 0;
      Date now = DateUtil.getDBCurrentTime();
      for (int i = 0; i < operRoleList.size(); i++)
      {
        operRoleVO = (OperRoleVO)operRoleList.get(i);
        tmp = this.operRoleDAO.queryoperRoleByPrimaryKey(managerId, sysCode, operRoleVO.getRoleId());
        if (tmp == null)
        {
          if (operRoleVO.getCreatedBy() == null)
          {
            operRoleVO.setCreatedBy("owenor");
            operRoleVO.setCreatedTime(now);
          }
          n = this.operRoleDAO.insertOperRole(operRoleVO);
        }
        else
        {
          if (operRoleVO.getUpdatedBy() == null)
          {
            operRoleVO.setUpdatedBy("owenor1");
            operRoleVO.setUpdatedTime(now);
          }
          n = this.operRoleDAO.updateOperRole(operRoleVO);
        }
        if (n <= 0) {
          throw new BusinessException(DzErrorCode.CommonErrorCode.SAVE_ERROR.value(), "插入或更新数据操作员角色信息失败", "插入或更新数据操作员角色信息失败");
        }
      }
    }
    catch (Exception e)
    {
      LogUtil.error("修改操作员指定业务系统的角色信息异常，参数为：managerId=" + managerId + ",sysCode=" + sysCode + ",operRoleList=" + operRoleList, e);
      throw e;
    }
  }
}
